﻿using System;
using System.Windows.Input;

namespace AppDesigner.Shared
{
    public class RelayCommand<T> : ICommand
    {
        private Action<T> _execute;
        private Func<T, bool> _canExecute;

        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }


        public RelayCommand(Action<T> exeucte, Func<T,bool> canExecute)
        {
            this._execute = exeucte;
            this._canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (parameter is T)
            {
                return _canExecute == null ? true : _canExecute((T)parameter);
            }
            return false;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (parameter is T)
            {
                _execute((T)parameter);
            }
            else
            {
                throw new ArgumentException("parameter is not type of T");
            }
        }
    }
}
